package com.example.daytest.mail;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

/**
 * @author heyu
 * @title: SendMail
 * @description:邮件发送
 * @date 2022/1/13下午3:31
 */
@Component
public class SendMail {




    @Value("${spring.mail.username}")
    private String from;

    @Autowired
    private JavaMailSender mailSender;




    @Scheduled(cron = "0/10 * * * * *")
    public void work(){
        this.sendMail("heyu5@asiainfo.com","ceshi","123");
    }



    public void sendMail(String to, String subject, String verifyCode) {
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom(from); //发送人
        message.setTo(to); //收件人
        message.setSubject(subject); //邮件名
        message.setText(verifyCode); //邮件内容（验证码）
        try {
            mailSender.send(message);
        }catch (Exception e){
            e.printStackTrace();
        }

    }
}
